<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace Plugin\Alarm\Http\Controller\Api;

use Plugin\Alarm\Service\RdAlarmWorkService;
use Plugin\Alarm\Http\Request\Admin\RdAlarmWorkRequest;
use Plugin\Alarm\Schema\RdAlarmWorkSchema;
use App\Http\Admin\Middleware\PermissionMiddleware;
use App\Http\Common\Middleware\AccessTokenMiddleware;
use App\Http\Common\Middleware\OperationMiddleware;
use App\Http\Common\Result;
use App\Http\CurrentUser;
use Hyperf\HttpServer\Request;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\Swagger\Annotation\Delete;
use Hyperf\Swagger\Annotation\Get;
use Hyperf\Swagger\Annotation\HyperfServer;
use Hyperf\Swagger\Annotation\JsonContent;
use Hyperf\Swagger\Annotation\Post;
use Hyperf\Swagger\Annotation\Put;
use Hyperf\Swagger\Annotation\RequestBody;
use Mine\Access\Attribute\Permission;
use Mine\Swagger\Attributes\PageResponse;
use App\Http\Admin\Controller\AbstractController as AdminAbstractController;

/**
 * 工单控制器
 * Class RdAlarmWorkApiController
 */
#[HyperfServer(name: 'http')]
final class RdAlarmWorkApiController extends AdminAbstractController
{

     /**
     * 业务处理服务
     * RdAlarmWorkService
     */
    public function __construct(
        protected readonly RdAlarmWorkService $service,
        protected readonly CurrentUser        $currentUser
    ) {}

    

    #[Get(
        path: '/alarm/rdAlarmWork/api/list',
        summary: '工单列表',
        security: [['Bearer' => [], 'ApiKey' => []]],
        tags: ['工单记录'],
    )]
    #[PageResponse(instance: RdAlarmWorkSchema::class)]
    #[Middleware(middleware: AccessTokenMiddleware::class, priority: 100)]
    public function page(Request $request): Result
    {
        return $this->success(data: $this->service->apiPage(array_merge([
            'user_id' => $this->currentUser->id(),
        ], $request->all()), (int) $request->query('page'), (int) $request->query('page_size')));
    }


    #[Post(
        path: '/alarm/rdAlarmWork/api/ai',
        summary: 'AI创建工单接口',
        security: [['Bearer' => [], 'ApiKey' => []]],
    )]
    #[RequestBody(
        content: new JsonContent(ref: RdAlarmWorkRequest::class, title: '创建工单记录')
    )]
    #[PageResponse(instance: new Result())]
    public function create(RdAlarmWorkRequest $request): Result
    {
        $this->service->create(array_merge($request->post(), [
            'created_by' => 0,
        ]));
        return $this->success();
    }

    #[Post(
        path: '/alarm/rdAlarmWork/api/ai/review/{id}',
        summary: 'AI复核工单接口',
        security: [['Bearer' => [], 'ApiKey' => []]],
    )]
    #[PageResponse(instance: new Result())]
    public function review(int $id, RdAlarmWorkRequest $request): Result
    {
        $this->service->review($id, $request->all());
        return $this->success();
    }

    #[Get(
        path: '/alarm/rdAlarmWork/api/detail/{id}',
        summary: '工单详情',
        security: [['Bearer' => [], 'ApiKey' => []]],
        tags: ['工单记录'],
    )]
    #[PageResponse(instance: RdAlarmWorkSchema::class)]
    #[Middleware(middleware: AccessTokenMiddleware::class, priority: 100)]
    public function detail(int $id): Result
    {
        return $this->success(data: $this->service->read($id, [
            'updated_by' => $this->currentUser->id(),
        ]));
    }

    #[Put(
        path: '/alarm/rdAlarmWork/api/handle/{id}',
        summary: '处置工单',
        security: [['Bearer' => [], 'ApiKey' => []]],
        tags: ['工单记录']
    )]
    #[PageResponse(instance: new Result())]
    #[Middleware(middleware: AccessTokenMiddleware::class, priority: 100)]
    public function save(int $id, Request $request): Result
    {
        $this->service->handle($id, array_merge($request->post(), [
            'updated_by' => $this->currentUser->id(),
        ]));
        return $this->success();
    }

    #[Put(
        path: '/alarm/rdAlarmWork/api/transfer/{id}',
        summary: '转移工单',
        security: [['Bearer' => [], 'ApiKey' => []]],
        tags: ['工单记录']
    )]
    #[PageResponse(instance: new Result())]
    #[Middleware(middleware: AccessTokenMiddleware::class, priority: 100)]
    public function transfer(int $id, Request $request): Result
    {
        $this->service->transfer2($id, array_merge($request->post(), [
            'updated_by' => $this->currentUser->id(),
        ]));
        return $this->success();
    }

    #[Put(
        path: '/alarm/rdAlarmWork/api/accept/{id}',
        summary: '接单工单',
        security: [['Bearer' => [], 'ApiKey' => []]],
        tags: ['工单记录']
    )]
    #[PageResponse(instance: new Result())]
    #[Middleware(middleware: AccessTokenMiddleware::class, priority: 100)]
    public function accept(int $id, Request $request): Result
    {
        $this->service->accept($id, [
            'updated_by' => $this->currentUser->id(),
            'status' => $request->post('status'),
        ]);
        return $this->success();
    }

    #[Get(
        path: '/alarm/rdAlarmWork/api/count',
        summary: '工单计数',
        security: [['Bearer' => [], 'ApiKey' => []]],
        tags: ['工单记录']
    )]
    #[PageResponse(instance: new Result())]
    #[Middleware(middleware: AccessTokenMiddleware::class, priority: 100)]
    public function count(): Result
    {
        return $this->success(data: $this->service->countTotal($this->currentUser->id()));
    }

    #[Put(
        path: '/alarm/rdAlarmWork/api/close/{id}',
        summary: '关闭工单',
        security: [['Bearer' => [], 'ApiKey' => []]],
        tags: ['工单记录']
    )]
    #[PageResponse(instance: new Result())]
    #[Middleware(middleware: AccessTokenMiddleware::class, priority: 100)]
    public function close(int $id, Request $request): Result
    {
        $this->service->close($id, [
            'updated_by' => $this->currentUser->id(),
            'remark' => $request->post('remark'),
        ]);
        return $this->success();
    }
}